package handler

import (
	"net/http"
  "bytes"
	"fmt"
	"io/ioutil"

	"github.com/zeromicro/go-zero/rest/httpx"
	"godco/internal/logic"
	"godco/internal/svc"
	"godco/internal/types"
)

func printRequest(r *http.Request) error {

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}

	r.Body = ioutil.NopCloser(bytes.NewBuffer(body))


	fmt.Printf("Request Method: %s\n", r.Method)


	fmt.Printf("Request URL: %s\n", r.URL)


	fmt.Println("Request Headers:")
	for name, values := range r.Header {
		for _, value := range values {
			fmt.Printf("%s: %s\n", name, value)
		}
	}
	fmt.Println("Request Body:")
	fmt.Println(string(body))

	return nil
}

func PushHookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.PushHookRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewPushHookLogic(r.Context(), svcCtx)
		resp, err := l.PushHook(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
